package org.example.config;


import org.example.controller.WelcomeController;
import org.example.interceptor.Log2Interceptor;
import org.example.interceptor.LogInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.handler.MappedInterceptor;

@Configuration
// 启用Spring MVC
@EnableWebMvc
// 启用组件扫描
@ComponentScan(basePackageClasses = WelcomeController.class)
public class WebConfig implements WebMvcConfigurer {

    // @Override
    // public void addInterceptors(InterceptorRegistry registry) {
    //     // Interceptor会按照添加顺序执行
    //     // 本例中，会先执行LogInterceptor，然后再执行Log2Interceptor
    //     registry.addInterceptor(new LogInterceptor()).addPathPatterns("/**");
    //     registry.addInterceptor(new Log2Interceptor()).addPathPatterns("/**");
    // }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 通过order方法明确指定顺序
        // LogInterceptor将比Log2Interceptor先执行
        registry.addInterceptor(new Log2Interceptor()).addPathPatterns("/**").order(2);
        registry.addInterceptor(new LogInterceptor()).addPathPatterns("/**").order(1);
    }

    // @Order注解并不能修改拦截器的执行顺序！！！
    // 此外，给拦截器实现Ordered接口也并不能修改拦截器的执行顺序！！！

    // @Order(1)
    // @Bean
    // public MappedInterceptor mappedInterceptor() {
    //     return new MappedInterceptor(new String[]{"/**"}, new LogInterceptor());
    // }
    //
    // @Order(100)
    // @Bean
    // public MappedInterceptor mappedInterceptor2() {
    //     return new MappedInterceptor(new String[]{"/**"}, new Log2Interceptor());
    // }

}

